<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class fournisseur_ctr2 extends CI_Controller {  
       
	    function __construct()
    {
        parent::__construct();
         $this->load->library("pagination");
		$this->load->helper(array('form', 'url'));
		$this->load->library('image_lib');
		$this->load->helper("file");
        $this->load->helper('form');
	
    }
	  
	 function index()
	{
		
		$this->index_test();
	}
	
	public function index_test(){
		
		 $this->load->library('session');
		$this->load->model('charticle_mdl'); // load the file welcome_mdl.php
		$this->load->model('fournisseur_mdl2'); // load the file welcome_mdl.php
		$charticle = $this->charticle_mdl->get_all(); // call the method filtera inside the class welcome_mdl
	
		 $data_charticle = array('data_charticle'=>$charticle); //ajouter la table r1 a data
		
		  $this->session->set_userdata($data_charticle); // mettre $data_charticle dans une var globale
	
			
				   $config = array();
          $config["base_url"] = base_url( "fournisseur_ctr2/index_test");
		  $config["total_rows"] =$this->fournisseur_mdl2->record_count_all();
		  $config["per_page"] = 20;
          $config["uri_segment"] = 3;
		  $config['first_link'] = 'PREMIER';
		  $config['last_link'] = 'DERNIER';
 

        $this->pagination->initialize($config);
		 $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
       $data["results"] = $this->fournisseur_mdl2->get_all3($config["per_page"], $page);
	   $data["results_recherche"] = $this->fournisseur_mdl2->get_id_per_nom();
        $data["links"] = $this->pagination->create_links();
		 $data["titre"] = 'قائمة الموردون';
		  $this->load->view('fournisseur_vew2',$data);  // send the result from the model to the view 
		
		
	}
	
	
			public function get_fournisseurs_per_article2($x1)
    {
		$this->load->model('fournisseur_mdl2'); // load the file welcome_mdl.php
	 $this->load->library('session');
	  $tab['val']=$x1;
	   $tab['nbr-enreg']= $this->fournisseur_mdl2->record_count($x1);
	   $tab['premier_appelle']=TRUE;
	  $this->session->set_userdata($tab); // mettre $tab dans une var globale
	  
	  

		
		$this->get_fournisseurs_per_article3();
		
	
	
    }
	
	public function get_fournisseurs_per_article3()
    {
		
	 $this->load->library('session');
        $x1=$this->session->userdata('val'); //access la var globale val

		$this->load->model('fournisseur_mdl2'); // load the file welcome_mdl.php
		$this->load->model('charticle_mdl'); // load the file welcome_mdl.php
		
			 /////pagination
		   $config = array();
         $config["base_url"] = base_url("fournisseur_ctr2/get_fournisseurs_per_article3");
		  $config["total_rows"] =$this->fournisseur_mdl2->record_count($x1);
		  $config["per_page"] = 20;
          $config["uri_segment"] = 3;
		 
		  
 //
       $this->pagination->initialize($config);
	   
	 $premier=$this->session->userdata('premier_appelle'); //access la var globale val
	 if($premier){
	 	$page=0;
		 $tab['premier_appelle']=FALSE;
		   $this->session->set_userdata($tab); // mettre $tab dans une var globale
		   }
	else $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
		
       $data["results"] = $this->fournisseur_mdl2->get_all2($config["per_page"], $page ,$x1);
	   $data["results_recherche"] = $this->fournisseur_mdl2->get_all_per_art($x1);
        $data["links"] = $this->pagination->create_links();
		$nom_art=$this->charticle_mdl->get_nom_article($x1);
		$nom_article=$nom_art[0]->nom;
		$data["titre"] ='قائمة الموردون الخاصة بالمادة :  '.$nom_article ;
		
		 $this->load->view("fournisseur_vew2",$data);	
		
		
		
	
	
    }
	
	public function find_name()
    {  
	   $this->load->library('session');
		$this->load->model('charticle_mdl'); // load the file welcome_mdl.php
		$this->load->model('fournisseur_mdl2'); // load the file welcome_mdl.php
		$charticle = $this->charticle_mdl->get_all(); // call the method filtera inside the class welcome_mdl
	
		 $data_charticle = array('data_charticle'=>$charticle); //ajouter la table r1 a data
		
		  $this->session->set_userdata($data_charticle); // mettre $data_charticle dans une var globale
		  
		  if($this->input->post('nom')) {
		  	$v=$this->input->post('nom');
			// $data_recherche_fourn = $v; //ajouter la table r1 a data
			 $data_recherche_fourn=array('data_fourn'=>$v);
		     $this->session->set_userdata($data_recherche_fourn); // mettre $data_charticle dans une var globale
			}
		  else{
		  	$v=$this->session->userdata('data_fourn'); //access it
		  }
			
				   $config = array();
          $config["base_url"] = base_url( "fournisseur_ctr2/find_name");
		  $config["total_rows"] =$this->fournisseur_mdl2->record_count_recherche($v);
		  $config["per_page"] = 20;
          $config["uri_segment"] = 3;
		  $config['first_link'] = 'PREMIER';
		  $config['last_link'] = 'DERNIER';
 

        $this->pagination->initialize($config);
		 $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
	   $data["results"] = $this->fournisseur_mdl2->get_id_per_nom_art($config["per_page"], $page,$v);
	   $data["results_recherche"] = $this->fournisseur_mdl2->get_id_per_nom();
        $data["links"] = $this->pagination->create_links();
		 $data["titre"] = 'قائمة الموردون المتضمن اسماؤهم : <br>'.$v;
		  $this->load->view('fournisseur_vew2',$data);  // send the result from the model to the view 
	
	
		}
	
	public function detail ($fournisseur_id,$msg){
		
		$this->load->model('fournisseur_mdl2');
		$this->load->model('activite_mdl');
		//$this->load->model('banque_mdl');
		
		
		$data1['rec'] = $this->fournisseur_mdl2->get_detaille_fournisseur($fournisseur_id); // get  one fournisseur per id
		$r_activity_id   = $this->activite_mdl->get_activity_per_fournisseur($fournisseur_id); // get one activity per fournisseur
		if($r_activity_id){
			$titre_activite = $r_activity_id[0]->titre;
		}else $titre_activite='';
		
		$data4['rec'] = $titre_activite;  //get one activity for the fournisseur  
		$data2['rec'] = $this->fournisseur_mdl2->get_all();   // get all  for autocomplete
		$data3['rec'] = $this->activite_mdl->get_all();  //get all activity list for autocomplete 
		//$data5['rec'] = $this->banque_mdl->get_all_banques(); // get the bank details for the fournisseur
		
		$r['fournisseur']    = $data1;
		$r['liste_fourn']    = $data2;
		$r['liste_activite'] = $data3;
		$r['activite']       = $data4;
		//$r['liste_bank']     = $data5;
		$r['erreurs']=$msg;
		$this->load->view('fournisseur_details_vew',$r); 
	}
	
	public function ajout ($tab,$errors){
		
		$this->load->model('fournisseur_mdl2');
		$this->load->model('activite_mdl');
		$this->load->model('banque_mdl');
		
		
		$data2['rec'] = $this->fournisseur_mdl2->get_all();   // get all  for autocomplete
		$data3['rec'] = $this->activite_mdl->get_all();  //get all activity list for autocomplete 
		$data4['rec'] = $this->banque_mdl->get_all_banques(); // get the bank details for the fournisseur

        
		$r['liste_fourn']    = $data2;
		$r['liste_activite'] = $data3;
		$r['liste_banques'] = $data4;
		$r['errors'] = $errors;
		$r['tab'] = $tab;

		
		$this->load->view('fournisseur_ajout_vew',$r); 
	}
	
	
		
	public function update_fournisseur($id)
	{
		$errors = array();
		// check for data concistency
		$phone= $this->input->post('phone');
		//$mobile= $this->input->post('mobile');
		$rc= $this->input->post('rc');
		$rib= $this->input->post('rib');
		$email= $this->input->post('email'); 
		$numero= $this->input->post('numero');
		$fax= $this->input->post('fax');
		$num_fisc= $this->input->post('num_fiscale'); 
		$nom= $this->input->post('nom'); 
		$address= $this->input->post('address'); 
		$type_activit= $this->input->post('type_activit');
		$wilaya= $this->input->post('wilaya'); 
		$banque= $this->input->post('banque'); 
		$adress_banque= $this->input->post('adress_banque'); 
		
		$this->load->model('activite_mdl');
		//$this->load->model('banque_mdl');
		$data = $this->activite_mdl->get_activite_per_nom($type_activit);
		//$data_banque = $this->banque_mdl->get_id_banque($banque);  
		if($data){
			$id_activite=$data[0]->id_activite;
		}else{
			$errors['activite'] = TRUE;
		}
		
		/*if($data_banque){// la banque existe deja
			$id_banque=$data_banque[0]->id_banque;
		}else{ // si la banque ne existe pas oncree une
			$la_banque = $this->banque_mdl->ajout_banques($banque,$addresse_banque); 
			$id_banque=$la_banque[0]->id_banque;
		}*/
		
		if (! $nom)
		{
			 $errors['nom'] = TRUE;
		}
		
		if (! $wilaya)
		{
			 $errors['wilaya'] = TRUE;
		}
		
		if (! $address)
		{
			 $errors['address'] = TRUE;
		}
		
		if (! $phone)
		{
			 $errors['phone'] = TRUE;
		}
		
		if (! $fax)
		{
			 $errors['fax'] = TRUE;
		}
		
		if (! $adress_banque)
		{
			 $errors['adress_banque'] = TRUE;
		}
		
		if (!$num_fisc)
		{
			 $errors['num_fiscale'] = TRUE;
		}
		/*if (!(preg_match('/\\(0(5|6|7)\\d{2}\\)-\\d{2}-\\d{2}-\\d{2}/', $mobile)))
		{
			 $errors['mobile'] = TRUE;
		}*/
		if (! $rc)
		{
			 $errors['rc'] = TRUE;
		}
		if (! $rib)
		{
			 $errors['rib'] = TRUE;
		}
		if($email != FALSE) // if email was typed check it's syntax
		{
			if (!(preg_match('/[a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}/', $email)))
			{
				 $errors['email'] = TRUE;
			}
		}
		
		/// if no errors do update
		if (empty($errors)) { 
		   // $this->banque_mdl->ajout_banques_fournisseur($id_banque,$id);
			$this->load->model('fournisseur_mdl2');
			$this->fournisseur_mdl2->update_fournisseur($id,$nom,$phone,$fax,$address ,$rc,$rib,$email,$id_activite,$wilaya,$banque,$num_fisc,$adress_banque); // get  one fournisseur per id
			
			echo"<script> alert(\"تمت عملية التحديث بنجاح\")</script>"; 
		    $this->detail($id,$errors);
		}
		else{
			// do not update
			$mm = 1;
						echo"<script> alert(\"هناك حقل ادخال غير مطابق\")</script>"; 
		
			$this->detail($id,$errors);
		}
		$vv=1; 
	}

   public function supprimer_fournisseur($id)
	{
		
		
		$this->load->model('fournisseur_mdl2');
		
		$tab_bc=$this->fournisseur_mdl2->existe_bc($id);
		$tab_conv=$this->fournisseur_mdl2->existe_convention($id);
		
		if(!$tab_bc AND !$tab_conv){
			$this->fournisseur_mdl2->supprimer_fournisseur($id);
			echo"<script> alert(\"تمت العملية بنجاح\")</script>"; 
			$this->index_test();
		}else{
			$errors=NULL;
			echo"<script> alert(\"لا يمكن حذف مورد لديه وصل طلب او اتفاقية\")</script>"; 
		    $this->detail($id,$errors);
		}
		
	
	}
	public function ajout_fournisseur()
	{
		//$this->load->model('banque_mdl');
		
		$errors = array();
		// check for data concistency
		$phone= $this->input->post('phone');
		//$mobile= $this->input->post('mobile');
		$rc= $this->input->post('rc');
		$rib= $this->input->post('rib');
		$email= $this->input->post('email'); 
		$numero= $this->input->post('numero');
		$fax= $this->input->post('fax');
		$num_fiscale= $this->input->post('num_fiscale'); 
		$nom= $this->input->post('nom'); 
		$address= $this->input->post('address'); 
		$type_activit= $this->input->post('type_activit');
		$wilaya= $this->input->post('wilaya'); 
		$banque= $this->input->post('banque'); 
		$adress_banque= $this->input->post('adress_banque'); 
		
		
		$this->load->model('activite_mdl');
		$data = $this->activite_mdl->get_activite_per_nom($type_activit);  //get all activity list for autocomplete 
		if($data) $id_activite=$data[0]->id_activite;
		else $errors['type_activit'] = TRUE;
		
		/*if($banque){
			
		$data_banque = $this->banque_mdl->get_id_banque($banque);  
			if($data_banque){// la banque existe deja
			$id_banque=$data_banque[0]->id_banque;
		}else{ // si la banque ne existe pas oncree une
			$la_banque = $this->banque_mdl->ajout_banques($banque); 
			$id_banque=$la_banque[0]->id_banque;
		}
		}else  $errors['banque'] = TRUE;
		*/
		
		if (! $nom)
		{
			 $errors['nom'] = TRUE;
		}
		
		if (! $wilaya)
		{
			 $errors['wilaya'] = TRUE;
		}
		
		if (! $address)
		{
			 $errors['address'] = TRUE;
		}
		
		if (! $phone)
		{
			 $errors['phone'] = TRUE;
		}
		
		if (! $fax)
		{
			 $errors['fax'] = TRUE;
		}
		
		if (! $adress_banque)
		{
			 $errors['adress_banque'] = TRUE;
		}
		
		if (!$num_fiscale)
		{
			 $errors['num_fiscale'] = TRUE;
		}
		/*if (!(preg_match('/\\(0(5|6|7)\\d{2}\\)-\\d{2}-\\d{2}-\\d{2}/', $mobile)))
		{
			 $errors['mobile'] = TRUE;
		}*/
		if (! $rc)
		{
			 $errors['rc'] = TRUE;
		}
		if (! $rib)
		{
			 $errors['rib'] = TRUE;
		}
		if($email != FALSE) // if email was typed check it's syntax
		{
			if (!(preg_match('/[a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}/', $email)))
			{
				 $errors['email'] = TRUE;
			}
		}
		
		
		/// if no errors do update
		if (empty($errors)) { 
		    
			 $this->load->model('fournisseur_mdl2');
			
			if($numero){// si le numero fournisseur est entree
				
			$existe_numero=$this->fournisseur_mdl2->test_numero($numero);
			// on test si le numero existe
			if($existe_numero) $errors['numero'] = TRUE;
			 // get  one fournisseur per id
			
			}else{// si numero non entree on cree un
				$tab_num=$this->fournisseur_mdl2->get_new_number();
				if($tab_num){
					$numero=$tab_num[0]->numero+1;
				}else $numero=1;
			}
			$tmp=$this->fournisseur_mdl2->ajout_fournisseur_new($nom,$phone,$fax,$rc,$rib,$email,$address,$id_activite,$numero,$wilaya,$num_fiscale,$banque,$adress_banque);
			$id=$tmp[0]->id_fournisseur;
			//$this->banque_mdl->ajout_banques_fournisseur($id_banque,$id);
			
		    $this->load->view('fournisseur_maj_success_vew');  // send the result from the model to the vie
			//$this->index();
		
		}
		else{
			// do not update
				echo"<script> alert(\"هناك حقل ادخال غير مطابق\")</script>"; 
			$tab['nom']=$nom;
			$tab['phone']=$phone;
			$tab['numero']=$numero;
			$tab['fax']=$fax;
			$tab['address']=$address;
			$tab['rc']=$rc;
			$tab['num_fiscale']=$num_fiscale;
			$tab['rib']=$rib;
			$tab['type_activit']=$type_activit;
			$tab['email']=$email;
			$tab['wilaya']=$wilaya;
			$tab['banque']=$banque;
			
			$this->ajout($tab,$errors);
		}
		$vv=1; 
	}

  
}
